import { post, get, postL } from '@/utils/http';
import { trans } from './base';
import { SERVICE_URL } from '@/utils/config';

export default {
  apis: {
    CATEGORY: SERVICE_URL + '/api/spu/basic/listCategoryLimit',
    LIST: SERVICE_URL + '/api/mallGoods',
    DETAIL: SERVICE_URL + '/api/mallGoods/detail',
    detailBySpuId: SERVICE_URL + '/api/mallGoods/detailBySpuId',

    PREVIEW_SUBMIT: SERVICE_URL + '/api/mallOrder/preSubmit',
    ORDERS: SERVICE_URL + '/api/mallOrder/pageMy',
    ORDER_DETAIL: SERVICE_URL + '/api/mallOrder/detail',
    APPLY: SERVICE_URL + '/api/mallOrder/submit',
    EXPRESS: SERVICE_URL + '/api/mallOrder/expressDetail',
    ORDER_PAY: SERVICE_URL + '/api/mallOrder/pay',
    RECEIVE: SERVICE_URL + '/api/mallOrder/receive',
    PACKAGES: SERVICE_URL + '/api/deliveryOrder/expressInfo',
    EXPRESS: SERVICE_URL + '/api/deliveryOrder/expressDetail',
    UPDATEPHONE: SERVICE_URL + '/api/mallOrder/updateRestNotifyMobile',
    SPIKELIST: SERVICE_URL + '/api/activity/mallSubject/subjectGoodsPage',
    SPRICETIMER: SERVICE_URL + '/api/activity/mallSubject/subject',
  },
  async categorys(limit = 5) {
    const res = await trans(get(this.apis.CATEGORY, { limit }));
    return res.data;
  },
  async list(param, loading = false) {
    const res = await trans(post(this.apis.LIST, { ...param }, { loading }));
    return res.data;
  },
  async detail(param, loading = false) {
    const res = await trans(get(this.apis.DETAIL, { ...param }, { loading }));
    return res.data;
  },
  async detailSpu(param, loading = false) {
    const res = await trans(
      get(this.apis.detailBySpuId, { ...param }, { loading })
    );
    return res.data;
  },
  async previewSubmit(param) {
    const res = await trans(postL(this.apis.PREVIEW_SUBMIT, param));
    return res.data;
  },
  async orders(current, size, tab, loading = false) {
    const res = await trans(
      post(this.apis.ORDERS, { current, tab, size }, { loading })
    );
    return res.data;
  },
  async orderDetail(tradeNo, loading = false) {
    const res = await trans(
      get(this.apis.ORDER_DETAIL, { tradeNo }, { loading })
    );
    return res.data;
  },
  async apply(param) {
    const res = await trans(postL(this.apis.APPLY, param));
    return res.data;
  },
  async orderPay(tradeNo, paymentType) {
    const res = await trans(
      postL(this.apis.ORDER_PAY, { tradeNo, paymentType })
    );
    return res.data;
  },
  async packages(tradeNo, loading = false) {
    const res = await trans(get(this.apis.PACKAGES, { tradeNo }, { loading }));
    return res.data;
  },

  async express(tradeNo, packageId, loading = false) {
    const res = await trans(
      get(this.apis.EXPRESS, { tradeNo, packageId }, { loading })
    );
    return res.data;
  },
  async receive(tradeNo) {
    const res = await trans(postL(this.apis.RECEIVE, { tradeNo }));
    return res.success;
  },
  async updatePhone(params) {
    const res = await trans(postL(this.apis.UPDATEPHONE, { ...params }));
    return res;
  },
  async spikeList(param, loading = false) {
    const res = await trans(
      postL(this.apis.SPIKELIST, { ...param }, { loading })
    );
    return res.data;
  },
  async spikeTimer(subjectId, loading = false) {
    const url = `${this.apis.SPRICETIMER}/${subjectId}`;
    const res = await trans(get(url, { loading }));
    return res.data;
  },
};
